package com.atguigu.service.Impl;

import com.atguigu.dao.AdminRoleDao;
import com.atguigu.dao.BaseDao;
import com.atguigu.dao.RoleDao;
import com.atguigu.entity.Role;
import com.atguigu.service.RoleService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title：
 * @author：Yu
 * @create：2023-02-01 12:17
 * @description：
 */
@DubboService
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private AdminRoleDao adminRoleDao;

    @Override
    public BaseDao<Role> getBaseDao() {
        return roleDao;
    }

    @Override
    public Map<String, List<Role>> getDateByAdminId(Long adminId) {
        //1.这里使用方法二：先查询所有的角色信息
        List<Role> all = roleDao.findAll();
        //2.再查询该用户所拥有的角色的id,由于是新的表，因此我们创建一个新的dao来实现该sql语句
        List<Long> idByAdminId = adminRoleDao.findIdByAdminId(adminId);
        //3.创建两个list,分别存放该用户为拥有的角色与该用户所拥有的角色
        List<Role> noAssignRoleList = new ArrayList<>();
        List<Role> assignRoleList = new ArrayList<>();
        //3.通过java来分类
        for (Role role : all) {
            if (idByAdminId.contains(role.getId())) {
                assignRoleList.add(role);
            } else {
                noAssignRoleList.add(role);
            }
        }
        Map<String, List<Role>> map = new HashMap<>();
        map.put("assignRoleList", assignRoleList);
        map.put("noAssignRoleList", noAssignRoleList);
        return map;
    }
}
